package com.yhg.qxmall.socket.app;

import java.util.Arrays;
import java.util.Map;


import com.alibaba.fastjson.JSONObject;
import com.yhg.qxmall.socket.to.SendInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yhg.qxmall.socket.entity.DailyEntity;
import com.yhg.qxmall.socket.service.DailyService;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 网站统计日数据
 *
 * @author 凌月青缃
 * @email linyueqingxiang@gmail.com
 * @date 2020-12-17 16:45:37
 */
@RestController
@RequestMapping("socket/daily")
public class DailyController {
    @Autowired
    private DailyService dailyService;
    @Resource
    protected HttpServletRequest request;

    @PostMapping("/saveBean")
    public R saveBean(@RequestBody String str){
        SendInfo parse =  JSONObject.parseObject(str, SendInfo.class);

        DailyEntity dailyEntity = new DailyEntity();
        dailyEntity.setArea(parse.getArea());
        dailyEntity.setDateCalculated(parse.getDateCalculated());
        dailyEntity.setTemperature(parse.getTemperature());
        dailyEntity.setHumidity(parse.getHumidity());
        dailyEntity.setDan(parse.getDan());
        dailyEntity.setJia(parse.getJia());
        dailyEntity.setLin(parse.getLin());

        System.out.println(dailyEntity);

        dailyService.save(dailyEntity);
        return R.ok();
    }

    /**
     * 日期的json数组，数量json数组
     * @param area
     * @param type
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/showData/{area}/{type}/{begin}/{end}")
    public R showData(@PathVariable String area,@PathVariable String type,@PathVariable String begin,
                      @PathVariable String end) {
        Map<String,Object> map = dailyService.getShowData(area,type,begin,end);
        return R.ok().put("data",map);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dailyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		DailyEntity daily = dailyService.getById(id);

        return R.ok().put("daily", daily);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DailyEntity daily){
		dailyService.save(daily);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DailyEntity daily){
		dailyService.updateById(daily);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		dailyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
